package com.why.blog.common.utils;

import sun.misc.BASE64Encoder;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.UUID;

/**
 * description:
 *
 * @author: taohan
 * @date: 2019年03月27日
 * @time: 16:36
 */
public class MD5Util {

	private static final String SALT = "yc_data_salt";
	
	/**
	 * MD5 加密
	 * @param content 加密内容
	 * @return 密文
	 */
	public static String encrypt(String content) {
		content += SALT;
		return encodeMd5(content.getBytes());
	}

	private static String encodeMd5(byte[] source) {
		try {
			return encodeHex(MessageDigest.getInstance("MD5").digest(source));
		} catch (NoSuchAlgorithmException e) {
			throw new IllegalStateException(e.getMessage(), e);
		}
	}

	private static String encodeHex(byte[] bytes) {
		StringBuffer buffer = new StringBuffer(bytes.length * 2);
		for (int i = 0; i < bytes.length; i++) {
			if (((int) bytes[i] & 0xff) < 0x10)
				buffer.append("0");
			buffer.append(Long.toString((int) bytes[i] & 0xff, 16));
		}
		return buffer.toString();
	}

	public static void main(String[] args) {
		System.out.println(encrypt("123123"));
	}
}
